package com.yehuishou.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.http.HttpRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yehuishou.util.KdApiOrderDistinguish;
import com.yehuishou.util.KdGoldAPIDemo;
import com.yehuishou.util.KdniaoTrackQueryAPI;

/**
 * 快递
 * @author VULCAN
 *
 */
@Controller
public class KdController {
		
	@RequestMapping("/Kd")
	public String kd() {
		return "Kd";
	}
	/**
	 * 
	 * @param ShipperCode	 快递公司编码  顺丰 编码 [ SF ]
	 * @param LogisticCode	 快递公司单号
	 * @return
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("/KdSelect")
	public String KdSelect(String LogisticCode){
		String yundanhao = LogisticCode;
		String danhao	 =yundanhao.toUpperCase();
		//即时查询
		KdniaoTrackQueryAPI kdjscx =new KdniaoTrackQueryAPI();
		//单号识别
		KdApiOrderDistinguish kdsb =new KdApiOrderDistinguish();
			String kdsbselect;
			try {
				//输入单号 接收返回的结果 获得 所属快递公司 编码  例如 EMS 
				kdsbselect = kdsb.getOrderTracesByJson(danhao);
				//返回的是 Map类型的 JSON格式数据 需要取到 里面的  ShipperCode 快递公司编码 参数 [需要先将他再转回JSONObject 对象]
				 JSONObject parseObject = JSON.parseObject(kdsbselect);
				 	//需要获取到的参数 ShipperCode 是在一个数组里面 首先 获得他的数组名 Shippers
			        JSONArray shippers = parseObject.getJSONArray("Shippers");
			        System.out.println(shippers);
			        //Object shipperCode = JSON.parseObject(shippers.getString(0)).get("ShipperCode").toString();
			        //得到数组名后 通过数组名 获取到下标为 0 的 对象 为 ShipperCode 并转为String 格式
			        String shipperCode = JSON.parseObject(shippers.getString(0)).get("ShipperCode").toString();
			        System.out.println(shipperCode);
			        String shipperName = JSON.parseObject(shippers.getString(0)).get("ShipperName").toString();
			        System.out.println(shipperName);
			        //将获得到的 快递公司编码 EMS 放入到 即时查询类中
			        String kdselect1 = kdjscx.getOrderTracesByJson(shipperCode, LogisticCode);
			        String kdselect=kdselect1.substring(0, kdselect1.length()-1)+","+"\"ShipperName\""+":"+"\""+shipperName+"\""+"}";
			        //返回JSON 到页面
			        System.out.println(kdselect);
			        
			        return kdselect;
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return "";					
	}
		
		/**
		 * 
		 * @param ShipperCode		
		 * @param PayType			
		 * @param ExpType			
		 * @param jName 			
		 * @param jMobile			
		 * @param jProvinceNam		
		 * @param jCityName			
		 * @param jExpAreaName		
		 * @param jAddress			
		 * @param GoodsName			
		 * @param assess_price		
		 * @param back_valids
		 * @return
		 */
		@RequestMapping("/yuyuequjian")
		public String yuyuequjian(String ShipperCode,Integer PayType,Integer ExpType,
				String jName,String jMobile,String jProvinceNam
				,String jCityName,String jExpAreaName,String jAddress,String GoodsName	
				) {
				
			//收件人信息  [固定参数]
				String sName 		="";
				String sMobile		="";
				String sProvinceNam ="";
				String sCityName    ="";
				String sExpAreaName ="";
				String sAddress		="";
			
			String OrderCode;
			SimpleDateFormat fmd = new SimpleDateFormat("yyyyMMddHHssmmSS");
			
			Thread tr =new Thread();
			
					synchronized (tr) {
						OrderCode = fmd.format(new Date());
						System.out.println(OrderCode);
					}
					
				KdGoldAPIDemo yuyuequjian =new KdGoldAPIDemo();
			try {
				String result =yuyuequjian.orderOnlineByJson(OrderCode,ShipperCode,PayType,ExpType,
						jName,jMobile,jProvinceNam,jCityName,jExpAreaName,jAddress,sName,sMobile,
						sProvinceNam,sCityName,sExpAreaName,sAddress,GoodsName);
				JSONObject parseObject = JSON.parseObject(result);
				System.out.println(parseObject);
				JSONObject  Order=(JSONObject) parseObject.get("Order");
				System.out.println("Order");
				//将返回的运单号填入到用户的运单信息中
				String LogisticCode =Order.getString("LogisticCode");
				System.out.println(LogisticCode);
				
				Boolean success = parseObject.getBoolean("Success");
				System.out.println(success);
				if(success.equals(true)) {
					return "index2";
				}
					
			} catch (Exception e) {
				
				e.printStackTrace();
			}
			return "";
		}
}
